package com.qmx.qmxapp.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.adapter.ItemClickAdapter;
import com.qmx.qmxapp.base.BaseFragment;
import com.qmx.qmxapp.base.DividerItemDecoration;
import com.qmx.qmxapp.bean.ClickEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class InitiationFragment extends BaseFragment {
    private View fragment_folder;
    private RecyclerView mRecyclerView;
    private ItemClickAdapter adapter;
    private SendMessageCommunitor sendMessage;

    public InitiationFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        sendMessage = (SendMessageCommunitor) activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        fragment_folder = inflater.inflate(R.layout.fragment_initiation, container, false);
        mRecyclerView = (RecyclerView) fragment_folder.findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
        initAdapter();
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.iv_status:
                        Toast.makeText(getActivity(), "iv_status" + position, Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.btn:
                        Toast.makeText(getActivity(), "btn" + position, Toast.LENGTH_SHORT).show();
                        break;
                }

                sendMessage.sendMessage("测试" + position);
            }
        });
        return fragment_folder;

    }

    private void initAdapter() {
        List<ClickEntity> data = new ArrayList<>();
        data.add(new ClickEntity(ClickEntity.CLICK_ITEM_VIEW, "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544676565756&di=bdc1b4bc1c9c558b766c90fc4e93034d&imgtype=0&src=http%3A%2F%2Fpic36.photophoto.cn%2F20150715%2F0007020080742327_b.jpg", "测试", "1", ""));
        data.add(new ClickEntity(ClickEntity.CLICK_ITEM_VIEW, "", "", "", ""));
        data.add(new ClickEntity(ClickEntity.CLICK_ITEM_VIEW, "", "", "", ""));
        data.add(new ClickEntity(ClickEntity.CLICK_ITEM_VIEW, "", "", "", ""));
        data.add(new ClickEntity(ClickEntity.CLICK_ITEM_VIEW, "", "", "", ""));
        data.add(new ClickEntity(ClickEntity.CLICK_ITEM_VIEW, "", "", "", ""));
        data.add(new ClickEntity(ClickEntity.CLICK_ITEM_VIEW, "", "", "", ""));
        data.add(new ClickEntity(ClickEntity.CLICK_ITEM_VIEW, "", "", "", ""));
        data.add(new ClickEntity(ClickEntity.CLICK_ITEM_VIEW, "", "", "", ""));
        adapter = new ItemClickAdapter(data);
        adapter.openLoadAnimation();
        mRecyclerView.setAdapter(adapter);
    }

    /**
     * Created by qin on 2016/1/1.
     * 用于fragment传递事件给activity
     */
    public interface SendMessageCommunitor {
        /**
         * 从fragment发送消息
         *
         * @param msg 消息内容
         */
        void sendMessage(String msg);
    }
}
